package com.windchimes.edueval_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.windchimes.edueval_platform.common.CustomException;
import com.windchimes.edueval_platform.domain.po.Teacher;
import com.windchimes.edueval_platform.domain.vo.TeacherVO;
import com.windchimes.edueval_platform.mapper.TeacherMapper;
import com.windchimes.edueval_platform.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.util.List;


/**
 * <p>
 * 教师表 服务实现类
 * </p>
 *
 * @author windchimes
 * @since 2025-04-04
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {
    @Override
    public Page<TeacherVO> getTeacherPage(int page, int pageSize, String name) throws CustomException {
        Page<Teacher> teacherPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(name),Teacher::getName,name)
                .orderByDesc(Teacher::getUpdateTime);
        this.page(teacherPage,queryWrapper);
        Page<TeacherVO> teacherVOPage = new Page<>();

        return (Page<TeacherVO>) teacherPage.convert(teacher -> {
                TeacherVO vo = new TeacherVO();
                BeanUtils.copyProperties(teacher,vo);
                return vo;
            });
    }


    @Override
    public TeacherVO getTeacherVOById(Long id) throws CustomException {
        Teacher teacher = this.getById(id);
        if (teacher == null) {
            throw new CustomException("没有查询到对应教师信息");
        }
        return TeacherVO.fromTeacher(teacher); // 实体转VO
    }

    @Override
    @Transactional
    public void setStatus(int status, List<Long> ids) throws CustomException {

        if (CollectionUtils.isEmpty(ids)) {
            throw new CustomException("ID列表不能为空");
        }

        if (status < 0 || status > 1) {
            throw new CustomException("状态值不合法");
        }
        lambdaUpdate()
                .in(Teacher::getId,ids)
                .set(Teacher::getStatus,status)
                .update();
    }


    /**
     * 教师登录
     * @return teacher
     */
    @Override
    public Teacher login(Teacher teacher) throws CustomException{

        String password = teacher.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        Teacher teacher1 = lambdaQuery().eq(Teacher::getPhone, teacher.getPhone()).one();


        if(teacher1==null){
            throw new  CustomException("教师不存在");
        }

        if(!teacher1.getPassword().equals(password)){
            throw new CustomException("密码错误");
        }
        if(teacher1.getStatus()==0){
            throw new CustomException("账号已禁用");
        }

        return teacher1; // 返回实体对象，而非字符串
    }


}
